﻿using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Sqlite;

namespace dbTest
{
	[Activity (Label = "dbTest", MainLauncher = true)]
	public class MainActivity : Activity
	{
		/// <summary>
		/// The mdtemp is object of MyDatabase class.
		/// </summary>
		SqliteDB mdTemp;
		/// <summary>
		/// The txtName, txtAge, txtCountry are object of EditText.
		/// </summary>
		EditText txtUser, txtPass, txtAllowS;
		/// <summary>
		/// The tvMsg is object of TextView.
		/// </summary>
		TextView tvMsg;
		/// <summary>
		/// The imgAdd, imgEdit, imgDelete, imgFind are object of ImageButton
		/// </summary>
		Button imgAdd, imgEdit, imgDelete, imgFind;
		/// <summary>
		/// Raises the create event for application.
		/// </summary>
		/// <param name='bundle'>
		/// Bundle.
		/// </param>
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it

			// initialization of database class object.
			mdTemp = new SqliteDB ("usuario_DB");
			// get ImageButton object instance from resource.
			imgAdd = FindViewById<Button> (Resource.Id.imgAdd);
			imgEdit = FindViewById<Button> (Resource.Id.imgEdit);
			imgDelete = FindViewById<Button> (Resource.Id.imgDelete);
			imgFind = FindViewById<Button> (Resource.Id.imgFind);
			// set images on image button from resource.
			/*	imgAdd.SetImageResource (Resource.Drawable.add);
			imgEdit.SetImageResource (Resource.Drawable.save);
			imgDelete.SetImageResource (Resource.Drawable.delete);
			imgFind.SetImageResource (Resource.Drawable.find); */
			// get EditText object instance from resource.
			txtUser = FindViewById<EditText> (Resource.Id.txtUser);
			txtPass = FindViewById<EditText> (Resource.Id.txtPass);
			txtAllowS = FindViewById<EditText> (Resource.Id.txtAllowS);
			tvMsg = FindViewById<TextView> (Resource.Id.tvMsg);

			tvMsg.Text = mdTemp.Message;
			// create ImageButton click event for imgAdd, imgEdit, imgDelete, imgFind. 
			imgAdd.Click += delegate {
				//database call add record function AddRecord().
				mdTemp.AddRecord (txtUser.Text, txtPass.Text ,int.Parse (txtAllowS.Text));
				tvMsg.Text = mdTemp.Message;
				txtUser.Text = txtPass.Text = txtAllowS.Text = "";
			};

			imgEdit.Click += delegate {
				int iId = -1;
				int.TryParse (tvMsg.Text, out iId);
				//database call update record function UpdateRecord().
				mdTemp.UpdateRecord (iId, txtUser.Text, txtPass.Text ,int.Parse (txtAllowS.Text));
				tvMsg.Text = mdTemp.Message;
				txtUser.Text = txtPass.Text = txtAllowS.Text = "";
			};
			imgDelete.Click += delegate {
				int iId = -1;
				int.TryParse (tvMsg.Text, out iId);
				//database call delete record function DeleteRecord().
				mdTemp.DeleteRecord (iId);
				tvMsg.Text = mdTemp.Message;
				txtUser.Text = txtPass.Text = txtAllowS.Text = "";
			};
			imgFind.Click += delegate {

				//database call search record function GetCursorView().
				string sColumnName = "";
				if (txtUser.Text.Trim () != "") {
					sColumnName = "user";
					GetCursorView (sColumnName, txtUser.Text.Trim ());
				} else
					if (txtPass.Text.Trim () != "") {
						sColumnName = "pass";
						GetCursorView (sColumnName, txtPass.Text.Trim ());
					} else
						if (txtAllowS.Text.Trim () != "") {
							sColumnName = "allowS";
							GetCursorView (sColumnName, txtAllowS.Text.Trim ());
						} else {
							GetCursorView ();
							sColumnName = "All";
						}
				tvMsg.Text = "Search by " + sColumnName + ".";
			};

			// get ListView object instance from resource and add ItemClick, EventHandler.
			ListView lvTemp = FindViewById<ListView> (Resource.Id.lvTemp);        
			lvTemp.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs> (ListView_ItemClick);
		}

		/// <summary>
		/// Lists the view_ item click.
		/// </summary>
		/// <param name='sender'>
		/// object sender.
		/// </param>
		/// <param name='e'>
		/// ItemClickEventArgs e.
		/// </param>
		void ListView_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
		{
			// get TextView object instance from resource layout record_view.axml.
			TextView tvIdShow = e.View.FindViewById<TextView> (Resource.Id.tvIdShow);
			TextView tvUserShow = e.View.FindViewById<TextView> (Resource.Id.tvUserShow);
			TextView tvPassShow = e.View.FindViewById<TextView> (Resource.Id.tvPassShow);
			TextView tvAllowSShow = e.View.FindViewById<TextView> (Resource.Id.tvAllowSShow);
			// read value and wirte in EditText object.
			txtUser.Text = tvUserShow.Text;
			txtPass.Text = tvPassShow.Text;
			txtAllowS.Text = tvAllowSShow.Text;
			//record id is write in TextView object to update or delete record.
			tvMsg.Text = tvIdShow.Text;
		}


		/// <summary>
		/// Gets the cursor view to show all record.
		/// </summary>
		protected void GetCursorView ()
		{
			Android.Database.ICursor icTemp = mdTemp.GetRecordCursor ();
			if (icTemp != null) {
				icTemp.MoveToFirst ();
				ListView lvTemp = FindViewById<ListView> (Resource.Id.lvTemp);
				string[] from = new string[] {"_id","user","pass","allowS" };
				int[] to = new int[] {
					Resource.Id.tvIdShow,
					Resource.Id.tvUserShow,
					Resource.Id.tvPassShow,
					Resource.Id.tvAllowSShow
				};
				// creating a SimpleCursorAdapter to fill ListView object.
				SimpleCursorAdapter scaTemp = new SimpleCursorAdapter (this, Resource.Layout.record_view, icTemp, from, to);
				lvTemp.Adapter = scaTemp;
			} else {
				tvMsg.Text = mdTemp.Message;
			}
		}
		/// <summary>
		/// Gets the cursor view.
		/// </summary>
		/// <param name='sColumn'>
		/// column filed of MyTable is Name,Age,Country.
		/// </param>
		/// <param name='sValue'>
		/// Value as user input.
		/// </param>
		protected void GetCursorView (string sColumn, string sValue)
		{
			Android.Database.ICursor icTemp = mdTemp.GetRecordCursor (sColumn, sValue);
			if (icTemp != null) {
				icTemp.MoveToFirst ();
				ListView lvTemp = FindViewById<ListView> (Resource.Id.lvTemp);
				string[] from = new string[] {"_id","user","pass","allowS" };
				int[] to = new int[] {
					Resource.Id.tvIdShow,
					Resource.Id.tvUserShow,
					Resource.Id.tvPassShow,
					Resource.Id.tvAllowSShow
				};
				// creating a SimpleCursorAdapter to fill ListView object.
				SimpleCursorAdapter scaTemp = new SimpleCursorAdapter (this, Resource.Layout.record_view, icTemp, from, to);
				lvTemp.Adapter = scaTemp;
			} else {
				tvMsg.Text = mdTemp.Message;
			}
		}

	}
}


